/*
 * Copyright 2011 The Rusted Roof, LLC.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package org.proteusframework.util;

import java.text.MessageFormat;
import java.util.Date;
import java.util.logging.Formatter;
import java.util.logging.LogRecord;

/**
 * Custom Java logger formatter.
 *
 * @author Tacoma Four
 */
public class BriefLogFormatter extends Formatter
{
    private static final MessageFormatFormatter formatter = new MessageFormatFormatter();

    private static final String EXCEPTION = "**EXCEPTION**";

    /**
     * A Custom format implementation that is designed for brevity.
     */
    public String format(LogRecord record)
    {
        return formatter.format(record);
    }

    private static class MessageFormatFormatter extends Formatter
    {

        private static final MessageFormat messageFormat = new MessageFormat("{0}[ {1,date,h:mm:ss} ] {2} >> {3} {4}\n");

        public MessageFormatFormatter()
        {
            super();
        }

        @Override
        public String format(LogRecord record)
        {
            StackTraceElement[] elements = new Throwable().getStackTrace();


            String where = "<unknown>";

            boolean isGlobalException = false;
            String exception = "";


            for (StackTraceElement element : elements)
            {
                if (!isGlobalException)
                {
                    if (!element.getClassName().startsWith("java.util.logging") &&
                            !element.getClassName().startsWith(BriefLogFormatter.class.getCanonicalName()))
                    {
                        if (element.getClassName().startsWith(GlobalExceptionHandler.class.getCanonicalName()))
                        {
                            isGlobalException = true;
                            // continue
                        } else
                        {
                            where = element.getFileName() + ":" + " [" + element.getMethodName() + "():"
                                    + element.getLineNumber() + "]";
                            break;
                        }
                    }
                } else
                {
                    StackTraceElement[] gehElements = GlobalExceptionHandler.lastThrowable.getStackTrace();
                    exception = EXCEPTION;
                    where = gehElements[0].toString();
                    break;
                }
            }


            Object[] arguments = new Object[6];
            arguments[0] = exception;
            arguments[1] = new Date(record.getMillis());
            arguments[2] = where;
            arguments[3] = record.getMessage();
            Object[] params = record.getParameters();
            if (null != params)
            {
                arguments[4] = params[0];
            } else
            {
                arguments[4] = "";
            }
            return messageFormat.format(arguments);
        }

    }
}
